﻿/*======================================*\
* Author: Vincent
* LastModifyUser: Vincent
* Description:  
* CreateTime： 2012-05
* Remark:  
\*======================================*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using AppCode.Util;
using AppCode.DbDTO;
using AppCode.Domain.Services;

namespace Vincent.AutoCode 
{
    public partial class frmDbConfig : Form
    {
        private DbConnInfo _item = null;
        private string _connStr = string.Empty;

        /// <summary>
        /// 通知配置是否已创建
        /// </summary>
        public bool ConfigIsCreate = false;
        public frmDbConfig()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSub_Click(object sender, EventArgs e)
        {
            _item = new DbConnInfo();
            string dbName = string.Empty;
            if (!string.IsNullOrEmpty(cmbDbList.Text))
            {
                dbName = string.Format("Initial Catalog={0};", cmbDbList.Text);
            }
            else
            {
                MessageBox.Show("请选择一个数据库(目前暂不支持批量数据库处理)！", "提 示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            _item.DbConnString =_connStr+dbName;
            if (!string.IsNullOrEmpty(cmbDbList.Text))
                _item.DbName = cmbDbList.Text;
            else
                _item.DbName = "全部库";
            _item.DbTypeInfo = AppCode.Contract.DbType.Sql2008;

            SerializeHelper.SerializeObjToXml<DbConnInfo>(_item, AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ConfigXml\\DbConfig.xml");
            this.Close();
            this.Dispose();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmDbConfig_Load(object sender, EventArgs e)
        {

        }

        #region 异步控件操作

        void WriteStateInfo(string text)
        {
            if (this.statusStrip1.InvokeRequired)
            {
                Action ac = () =>
                {
                    this.statusStrip1.Text = text;
                };
                this.statusStrip1.BeginInvoke(ac);
            }
            else
            {
                this.statusStrip1.Text = text;
            }
        }



        #endregion

        /// <summary>
        /// 测试连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnect_Click(object sender, EventArgs e)
        {
            string serverIp = cmbDbServer.Text.Trim();
            string userName = txtUserName.Text.Trim();
            string userPassWord = txtPassWord.Text.Trim();

            int connTimeOut = 30;
            _connStr = string.Format("Data Source={0};User ID={1};PassWord={2};",serverIp,userName,userPassWord);
            var db = new DbConfigHandler(_connStr);
            List<string> list = null;
            Func<List<string>> func = () =>
            {
                list = db.GetDataBaseName();
                return list;
            };
            IAsyncResult result = func.BeginInvoke((a) =>
            {
                IAsyncResult tmp = (IAsyncResult)a;
                int i =0;
                while (!tmp.IsCompleted)
                { 
                    statusInfo.Text = "连接...";
                    WriteStateInfo("连接...,尝试次数"+i);
                    Thread.Sleep(1000);
                    connTimeOut--;
                }
                if (connTimeOut > 0 && list != null && list.Count > 0)
                {
                    if (cmbDbList.InvokeRequired)
                    {
                        Action ac = () =>
                        {
                            cmbDbList.Items.Clear();
                            list.ForEach(b => cmbDbList.Items.Add(b.ToString()));
                        };
                        cmbDbList.BeginInvoke(ac);
                    }
                    else
                    {
                        cmbDbList.Items.Clear();
                        list.ForEach(b => cmbDbList.Items.Add(b.ToString()));
                    }
                    if (btnSub.InvokeRequired)
                    {
                        Action vc = () =>
                        {
                            this.btnSub.Enabled = true;
                        };
                        btnSub.BeginInvoke(vc);
                    }
                    else
                    {
                        this.btnSub.Enabled = true;
                    }
                    MessageBox.Show("连接成功,请选择数据库!", "信息", MessageBoxButtons.OK);
                }
                else
                {
                    if (this.InvokeRequired)  //非主线程
                    {
                        Action ac = () =>
                        {
                            MessageBox.Show("连接失败！请检查配置信息是否正确!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        };
                        this.BeginInvoke(ac);
                    }
                    else
                    {
                        MessageBox.Show("连接失败！请检查配置信息是否正确!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }, null);
            
        }
    }
}
